<?php

FLEA::loadClass('FLEA_Controller_Action');

class Controller_Admin_roles extends FLEA_Controller_Action
{
	/**
	 * $md_roles
	 * @var Model_RolesManager
	 */
	var $md_roles;
	
	function Controller_Admin_roles()
	{
		$this->md_roles =& FLEA::getSingleton('Model_RolesManager');
	}
	function actionIndex()
	{
		redirect($this->_url('rolesList'));
	}
	
	/**
	 * 角色列表
	 */
	function actionrolesList()
	{
		//组织数据
		$viewdata = array(
			// 全部角色列表
			'rolesList' => $this->md_roles->rolesList(),
			// title标签
			'title' => '角色管理',
			// 当前菜单标签
			'menu_roles' => 'id="current"',
	    );
	    
		$this->_executeView('admin/Websetup_roles.html', $viewdata);						
	}
	
	/**
	 * 更新角色
	 */
	function actionroleUpdate()
	{
		$this->_jumpto($this->md_roles->updateRole($_POST));
	}
	
	/**
	 * 创建新角色
	 */
	function actionroleCreate()
	{
		$this->_jumpto($this->md_roles->createRole($_POST));
	}
	
	/**
	 * ajax方式删除一条角色记录
	 */
	function actionajaxroleRemove()
	{
		echo $this->md_roles->removeRole($_GET['role_id']);
	}
	
	/**
	 * 角色绑定权限页面
	 */
	function actionband(){
		// 检查传入值
		$role = $this->md_roles->_checkRole($_GET['role_id']);
		if(empty($_GET['role_id']) or !$role){
			$this->_jumpto(false);
		}elseif ($role === 'admin'){
			$this->_jumpto(false,'admin角色具有全部权限，无需绑定');
		}
		
		$acl =& FLEA::getSingleton('Model_AclManager');
		/*@var $acl Model_AclManager*/
		$md_User =& FLEA::getSingleton('Model_UserManager');
		/*@var $md_user Model_UserManager*/
		
		//组织数据
		$viewdata = array(
			'thisrole' => $acl->loadRole($_GET['role_id']),
			'permissionsList' => $md_User->permissionsList(),
			'title' => '角色绑定权限',
			'menu_roles' => 'id="current"',
	    );
		//dump($viewdata);
	    $this->_executeView('admin/Websetup_rolesband.html', $viewdata);
	}
	
	/**
	 * 保存角色绑定
	 */
	function actionsaveband(){
		$acl =& FLEA::getSingleton('Model_AclManager');
		/*@var $acl Model_AclManager*/
		$acl->saveroleBand($_POST);
		redirect($this->_url('band',array('role_id'=>$_POST['role_id'])));
	}
	
	
	/**
	 * 判断错误并实施页面跳转
	 *
	 * @param boolin $stat
	 * @param string $err_text
	 * @param url $to_url
	 * @param url $err_url
	 */
	function _jumpto($stat, $err_text=null, $to_url=null, $err_url=null)
	{
		$err_text = isset($err_text) ? $err_text : "有错误发生！";
		$to_url = isset($to_url) ? $to_url : $this->_url('index');
		$err_url = isset($err_url) ? $err_url : $to_url;
		
		if($stat){
			redirect($to_url);
		}else{
			js_alert($err_text, '', $err_url);
		}
	}
	
	
	
}

?>